package com.jeesite.modules.rt.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.rt.dao.RtDeviceShouyiDao;
import com.jeesite.modules.rt.dao.RtErrorDao;
import com.jeesite.modules.rt.dto.DeviceShouyiDto;
import com.jeesite.modules.rt.entity.RtDevice;
import com.jeesite.modules.rt.entity.RtDevicePaoliang;
import com.jeesite.modules.rt.entity.RtError;
import com.jeesite.modules.rt.service.RtDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.rt.entity.RtDeviceShouyi;
import com.jeesite.modules.rt.service.RtDeviceShouyiService;

/**
 * 收益Controller
 * @author 李寒冰
 * @version 2024-10-16
 */
@Controller
@RequestMapping(value = "${adminPath}/rt/rtDeviceShouyi")
public class RtDeviceShouyiController extends BaseController {

	@Autowired
	private RtDeviceShouyiService rtDeviceShouyiService;
    @Autowired
    private RtDeviceService rtDeviceService;
    @Autowired
    private RtDeviceShouyiDao rtDeviceShouyiDao;
	@Autowired
	private RtErrorDao rtErrorDao;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public RtDeviceShouyi get(String id, boolean isNewRecord) {
		return rtDeviceShouyiService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("rt:rtDeviceShouyi:view")
	@RequestMapping(value = {"list", ""})
	public String list(RtDeviceShouyi rtDeviceShouyi, Model model) {
		rtDeviceShouyi.setWorkDate_gte_date(DateUtils.parseDate(DateUtils.getDate("yyyy-MM-dd",-1 ,Calendar.DATE)));
		rtDeviceShouyi.setWorkDate_lte_date(new Date());
		model.addAttribute("rtDeviceShouyi", rtDeviceShouyi);
		return "modules/rt/rtDeviceShouyiList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("rt:rtDeviceShouyi:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<RtDeviceShouyi> listData(RtDeviceShouyi rtDeviceShouyi, HttpServletRequest request, HttpServletResponse response) {
		rtDeviceShouyi.setPage(new Page<>(request, response));
		Page<RtDeviceShouyi> page = rtDeviceShouyiService.findPage(rtDeviceShouyi);
		for (RtDeviceShouyi item : page.getList()) {
			String sn = item.getSn();
			RtDevice deviceBySn = rtDeviceService.getDeviceBySn(sn);
			String online = deviceBySn.getOnline();
			item.setOnlineCode(Long.valueOf(online));
			String errorTypesBySn = rtErrorDao.getErrorTypesBySn(sn);
			item.setErrorStr(errorTypesBySn);
			item.setRtDevice(deviceBySn);
		}
		return page;
	}

	/**
	 * 导出跑量数据
	 */
	@RequiresPermissions("rt:rtDevicePaoliang:view")
	@RequestMapping(value = "exportData")
	public void exportData(RtDeviceShouyi rtDeviceShouyi,HttpServletRequest request, HttpServletResponse response) {

		if(StringUtils.isEmpty(request.getParameter("workDate_gte")) || StringUtils.isNotEmpty(request.getParameter("workDate_gte"))){
            try {
				response.setCharacterEncoding(EncodeUtils.UTF_8);
                PrintWriter writer = response.getWriter();

				JSONObject jsonObject = new JSONObject();
				jsonObject.put("result","false");
				jsonObject.put("message","请填写时间区间");
				writer.write(jsonObject.toJSONString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
		rtDeviceShouyi.setPage(null);
		List<DeviceShouyiDto> list = rtDeviceShouyiDao.daochu(rtDeviceShouyi);
		Date workDate = rtDeviceShouyi.getWorkDate();
		String date = null;
		if(workDate==null){
			date = DateUtils.getDate("yyyyMMdd");
		}else{
			date = DateUtils.formatDate(workDate,"yyyyMMdd");
		}
		String fileName = "收益数据" + date + ".xlsx";
		try(ExcelExport ee = new ExcelExport("收益数据", DeviceShouyiDto.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("rt:rtDeviceShouyi:view")
	@RequestMapping(value = "form")
	public String form(RtDeviceShouyi rtDeviceShouyi, Model model) {
		model.addAttribute("rtDeviceShouyi", rtDeviceShouyi);
		return "modules/rt/rtDeviceShouyiForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("rt:rtDeviceShouyi:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated RtDeviceShouyi rtDeviceShouyi) {
		rtDeviceShouyiService.save(rtDeviceShouyi);
		return renderResult(Global.TRUE, text("保存收益成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("rt:rtDeviceShouyi:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(RtDeviceShouyi rtDeviceShouyi) {
		rtDeviceShouyiService.delete(rtDeviceShouyi);
		return renderResult(Global.TRUE, text("删除收益成功！"));
	}
	
}